package cn.zcib.film.controller;

import cn.zcib.film.anno.Record;
import cn.zcib.film.bean.User;
import cn.zcib.film.bean.vo.UserVO;
import cn.zcib.film.service.UserService;
import cn.zcib.film.util.DataGridViewResult;
import cn.zcib.film.util.SystemConstants;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 查询用户列表
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult findSubs(UserVO userVO){
        //设置分页信息
        PageHelper.startPage(userVO.getPage(),userVO.getLimit());
        //处理查询条件
        String uAccount = userVO.getuAccount();
        if (uAccount!=null & uAccount !=""){
            userVO.setuAccount("%"+uAccount+"%");

        }
        //调用查询电影方法
        List<User> users = userService.findUsers(userVO);

        //创建分页对象
        PageInfo<User> pageInfo = new PageInfo<>(users);

//        System.out.println("pageInfo = " + pageInfo);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("/update")
    public String  updateUser(User user){
        HashMap<String, Object> map = new HashMap<>();

        if (userService.updateUser(user)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "修改成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "修改失败");
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping("/reset")
    public String  resetPassword(Integer uId){
        HashMap<String, Object> map = new HashMap<>();

        if (userService.resetPassword(uId)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "重置成功，密码：123456");
            System.out.println("map = " + map);
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "重置失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/del")
    public String  delUser(Integer uId){
        HashMap<String, Object> map = new HashMap<>();

        if (userService.deleteUser(uId)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "删除成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/genderPer",produces = {"application/json;charset=UTF-8"})
    public String  countUser(){
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String, Integer>> list = userService.userGenderPer();

        for (Map<String,Integer> node:list
             ) {
//            System.out.println("node = " + node);
            map.put(String.valueOf(node.get("gender")), node.get("count"));
        }
//        System.out.println("map = " + map);
        return JSON.toJSONString(map);
    }
}
